package com.kizitonwose.calendarview;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiuQingHua
 * @version 1.0
 * @date 2021/2/18 19:00
 */
public abstract class QuickBaseItemProvider<E> extends BaseItemProvider {
    private List<E> list;
    private Context context;

    public QuickBaseItemProvider(Context context) {
        this(context,new ArrayList<>());
    }

    public QuickBaseItemProvider(Context context,List<E> list) {
        this.list = list;
        this.context = context.getApplicationContext();
    }

    public Context getContext() {
        return context;
    }

    public List<E> getList() {
        return list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public E getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        BaseComponentHolder holder;
        if (component == null) {
            holder = createComponentHolder(componentContainer, getItemComponentType(i));
            holder.getComponentById().setTag(holder);
        } else {
            holder = (BaseComponentHolder) component.getTag();
        }
        bindComponentHolder(holder, i, getItemComponentType(i));
        return holder.getComponentById();
    }

    private BaseComponentHolder createComponentHolder(ComponentContainer componentContainer, int componentType) {
        return new BaseComponentHolder(context,getComponentResourceLayout(componentContainer, componentType)) {
        };
    }

    protected abstract int getComponentResourceLayout(ComponentContainer componentContainer, int componentType);

    protected abstract void bindComponentHolder(BaseComponentHolder holder, int position, int componentType);

    /**
     * @author LiuQingHua
     * @version 1.0
     * @date 2021/2/4/004 19:00
     */
    public abstract static class BaseComponentHolder {
        protected Component component;
        protected Map<Integer, Component> views = new HashMap<>();

        public BaseComponentHolder(Context context,int layoutResId) {
            this.component = LayoutScatter.getInstance(context).parse(layoutResId, null, false);
        }

        public Component getComponentById() {
            return component;
        }

        public BaseComponentHolder setText(int id, String str) {
            Text text = getComponentById(id);
            text.setText(str);
            return this;
        }

        public BaseComponentHolder setPixelMap(int id, PixelMap pixelMap) {
            Image image = getComponentById(id);
            image.setPixelMap(pixelMap);
            return this;
        }

        public BaseComponentHolder setBackground(int id, Element element) {
            getComponentById(id).setBackground(element);
            return this;
        }

        /**
         * @noinspection unchecked
         */
        public  <T extends Component> T getComponentById(int id) {
            T t;
            if (!views.containsKey(id)) {
                t = findComponentById(id);
                views.put(id, t);
            } else {
                t = (T) views.get(id);
            }
            return t;
        }

        /**
         * @noinspection unchecked
         */
        private  <T extends Component> T findComponentById(int id) {
            return (T) component.findComponentById(id);
        }


    }
}
